package com.markby.fscm.cryptogram.domain.standard0016;

import lombok.Data;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;

import static com.markby.fscm.cryptogram.domain.standard0016.Constant0016.ECC_MAX_MODULUS_BITS_LEN;


/**
 * 0016-2023规范 ECC私钥数据结构
 * ULONG 为32位无符号整数
 * typedef struct Struct_ECCPRIVATEKEYBLOB{
 * ULONG 	BitLen;									// 	模数的实际位长度(应是8的倍数) 256或者512
 * BYTE	PrivateKey[ECC_MAX_MODULUS_BITS_LEN/8];	//	私有密钥（有限域上的整数 #define ECC_MAX_MODULUS_BITS_LEN 512）
 * }ECCPRIVATEKEYBLOB,*PECCPRIVATEKEYBLOB;
 * ECC_MAX_MODULUS_BITS_LEN为ECC算法模数的长度。
 */
@Data
public class ECCPRIVATEKEYBLOB {
    private int BitLen;
    private byte[] PrivateKey = new byte[ECC_MAX_MODULUS_BITS_LEN / 8];

    public ECCPRIVATEKEYBLOB(BCECPrivateKey bcecPrivateKey, int bitLen) {
        this.BitLen = bitLen;
        byte[] dBytes = bcecPrivateKey.getD().toByteArray();

//        if (dBytes.length > ECC_MAX_MODULUS_BITS_LEN / 8) {
//            throw new IllegalArgumentException("Invalid PrivateKey length: " + dBytes.length);
//        }

        if (bitLen == 256) {
            int len = ECC_MAX_MODULUS_BITS_LEN / 8 / 2;
            System.arraycopy(dBytes, dBytes.length - len, PrivateKey, PrivateKey.length - len, len);
        } else if (bitLen == 512) {
            int len = ECC_MAX_MODULUS_BITS_LEN / 8;
            System.arraycopy(dBytes, dBytes.length - len, PrivateKey, PrivateKey.length - len, len);
        }
    }


}
